########################################################
## PROGRAM NAME: 000_reformat_inputs.R                ##
## AUTHOR: MATT MLECZKO                               ##
## INPUTS:                                            ##
##    Raw NZLUD zoning data                           ##
##                                                    ##
## OUTPUTS:                                           ##
##    Reformatted NZLUD zoning data                   ##
##                                                    ##
## PURPOSE: Remove filepaths from input zoning data   ##
##                                                    ##
## LIST OF UPDATES:                                   ##
########################################################

## load libraries ## 

library(tidyverse)
library(readxl)

## define paths ##
input_path <- # USER DEFINED PATH HERE #

## set working directory
setwd(input_path)

## start with most of the data ## 

zfile.list <- list.files(path = paste0(input_path,
                                       "NZLUD/"),
                         pattern='*_fin.xls')

zfile.list.pad <- paste0("NZLUD/",zfile.list)

zlist <- lapply(zfile.list.pad, read_excel)

zd.2022 <- bind_rows(zlist) %>%
  mutate(muni_pr = substr(muni, 46, nchar(muni)-4)) %>%
  select(-muni) %>%
  rename(muni = muni_pr) %>%
  select(muni, 
         everything())

## now, additional raw data ## 

manual.codes <- read_excel("manual_codes.xls")
manual.codes$muni <- "PA_Midland"

ccs1 <- read_excel("nzlud_022220230.xls")
ccs2 <- read_excel("nzlud_022220231.xls")
ccs3 <- read_excel("nzlud_022220232.xls")
ccs4 <- read_excel("nzlud_022220233.xls")

ccs.pt1 <- rbind(ccs1,
                 ccs2,
                 ccs3,
                 ccs4)

ccs.pt1.rf <- ccs.pt1 %>%
  mutate(muni_pr = substr(muni, 60, nchar(muni)-4)) %>%
  select(-muni) %>%
  rename(muni = muni_pr) %>%
  select(muni, 
         everything())

ccs5 <- read_excel("sf_msa_adds.xls")
ccs5 <- filter(ccs5, grepl("Oakland",muni))

ccs.pt2.rf <- ccs5 %>%
  mutate(muni_pr = substr(muni, 46, nchar(muni)-4)) %>%
  select(-muni) %>%
  rename(muni = muni_pr) %>%
  select(muni, 
         everything())

ccs6 <- read_excel("nzlud_0523.xls")

ccs.pt3.rf <- ccs6 %>%
  mutate(muni_pr = substr(muni, 49, nchar(muni)-4)) %>%
  select(-muni) %>%
  rename(muni = muni_pr) %>%
  select(muni, 
         everything())

ccs7 <- read_excel("beloitcity.xls")

ccs.pt4.rf <- ccs7 %>%
  mutate(muni_pr = substr(muni, 64, nchar(muni)-4)) %>%
  select(-muni) %>%
  rename(muni = muni_pr) %>%
  select(muni, 
         everything())

## now, combine all the data ##

zoning.data.out <- rbind(zd.2022,
                         manual.codes,
                         ccs.pt1.rf,
                         ccs.pt2.rf,
                         ccs.pt3.rf,
                         ccs.pt4.rf) %>%
  rename(mf_per = `mf per`)


## export ##

write.csv(zoning.data.out,
          file = "nzlud.csv")


## END OF PROGRAM ##







